//########################################################################
//## (C) by Edwin Vriethoff under Microsoft Reciprocal License (Ms-RL)  ##
//## See http://www.codeplex.com/SPUserPoll/license for full license    ##
//##                                                                    ##
//## http://edwin.vriethoff.net http://www.codeplex.com/SPUserPoll/     ##
//########################################################################

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using SPUserPoll.Business.Components;

namespace SPUserPoll.Presentation.WebParts.UserControls
{
    public partial class PollAnswerControl : System.Web.UI.UserControl
    {
        #region Properties
        private Control _pollingWebpart = null;

        /// <summary>
        /// Access to the polling webpart
        /// </summary>
        private PollDisplayWebPart PollingWebPart
        {
            get
            {
                if (_pollingWebpart == null)
                    _pollingWebpart = this;

                if (_pollingWebpart.GetType() != typeof(PollDisplayWebPart))
                {
                    _pollingWebpart = this;
                    while (_pollingWebpart.GetType() != typeof(PollDisplayWebPart))
                    {
                        _pollingWebpart = _pollingWebpart.Parent;
                    }
                }
                return _pollingWebpart as PollDisplayWebPart;
            }
        }


        /// <summary>
        /// The Id of the Poll
        /// </summary>
        private string PollID
        {
            get { return PollingWebPart.SPUserPollID; }
        }

        /// <summary>
        /// Thd ID of the WebPart
        /// </summary>
        private string WebPartID
        {
            get { return PollingWebPart.ID; }
        }

        /// <summary>
        /// Question of the poll
        /// </summary>
        private string PollQuestion
        {
            get { return PollingWebPart.SPUserPollQuestion; }
        }

        /// <summary>
        /// Show or hide the view responses link
        /// </summary>
        private bool HideViewResultLink
        {
            get { return PollingWebPart.SPUserPollHidePreviewResponsesLink; }
        }

        /// <summary>
        /// The poll publishing status
        /// </summary>
        private bool PollPublished
        {
            get { return PollingWebPart.SPUserPollPublished; }
        }

        /// <summary>
        /// Poll result type
        /// </summary>
        private Types.PollResultType PollResultType
        {
            get { return PollingWebPart.SPUserPollResultType; }
        }
        #endregion

        #region Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                litQuestion.Text = PollQuestion;

                bool useCache = true;
                //do not cache answers in case that the user is an admin, designer or owner.
                if (PollingWebPart.UserIsAdmin || PollingWebPart.UserIsDesigner || PollingWebPart.UserIsOwner)
                {
                    useCache = false;
                }

                if (HideViewResultLink == true)
                    phViewResultLink.Visible = false;

                //load the poll answers
                if (PollingWebPart.SPUserPollAllowMultipleAnswers == false)
                {
                    //use radio buttons
                    rblAnswers.DataSource = ManageLists.GetAnswersForPoll(PollID, useCache);
                    rblAnswers.DataBind();
                    rblAnswers.Visible = true;
                    cblAnswers.Visible = false;
                }
                else
                {
                    //use checkboxes
                    cblAnswers.DataSource = ManageLists.GetAnswersForPoll(PollID, useCache);
                    cblAnswers.DataBind();
                    cblAnswers.Visible = true;
                    rblAnswers.Visible = false;
                }
            }
        }

        public void LoadResultWebPart()
        {
            //show the control  
            phAnswerControls.Visible = false;
            phResultControl.Visible = true;
        }

        protected void lbReturn_Click(object sender, EventArgs e)
        {
            //Validate the action!
            SPUtility.ValidateFormDigest();

            phResultControl.Visible = false;
            phAnswerControls.Visible = true;
        }

        protected void lbViewResults_Click(object sender, EventArgs e)
        {
            //Validate the action!
            SPUtility.ValidateFormDigest();

            //User has already voted. We show the poll results
            LoadResultWebPart();
        }

        protected void lbVote_Click(object sender, EventArgs e)
        {
            //Validate the action!
            SPUtility.ValidateFormDigest();

            //check if the user made a selection
            if (PollingWebPart.SPUserPollAllowMultipleAnswers == false && string.IsNullOrEmpty(rblAnswers.SelectedValue))
            {
                //no value selected. display a warning message
                litError.Text = "Please select an answer." + "<br/><br/>";
                return;
            }
            if (PollingWebPart.SPUserPollAllowMultipleAnswers == true)
            {
                //we need to check the checkbox values
                if (string.IsNullOrEmpty(cblAnswers.SelectedValue))
                {
                    litError.Text = "Please select an answer." + "<br/><br/>";
                    return;
                }
            }

            //regiser the vote
            string userName = User.GetUserLogin();

            if (PollingWebPart.SPUserPollAllowMultipleAnswers == false)
            {
                //single response
                int userVote = Convert.ToInt32(rblAnswers.SelectedValue);
                ManageLists.RegisterVote(userName, userVote, PollID, PollingWebPart.SPUserPollAnonymousVotes);
            }
            else
            {
                //Multiple answers
                foreach (ListItem item in cblAnswers.Items)
                {
                    if (item.Selected)
                    {
                        int userVote = Convert.ToInt32(item.Value);
                        ManageLists.RegisterVote(userName, userVote, PollID, PollingWebPart.SPUserPollAnonymousVotes);
                    }
                }
            }

            //reload the page for correct display of the poll
            HttpContext.Current.Response.Redirect(Page.Request.Url.ToString());
        }
        #endregion
    }
}